package ru.spbu.math.android.cbrf.view;

import android.app.Activity;
import android.app.ProgressDialog;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import ru.spbu.math.android.cbrf.dataprovider.ProviderManager;
import ru.spbu.math.android.cbrf.model.RateType;

/**
 * Created by IntelliJ IDEA. User: sandman Date: 19.03.2010 Time: 10:17:05 To
 * configure what kind of data should take a widget place
 */

public class ItemsConfigurationActivity extends Activity implements Runnable{

	private String LOG_TAG = "ItemsConfigurationActivity";
	private int appWidgetId;// the ID of widget that starts this Activity

	private CategoryListAdapter categoryListAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setResult(RESULT_CANCELED);
		
//		ProgressDialog loadingDialog = ProgressDialog.show(this, "", 
//                "Loading. Please wait...");
		
//		Thread thread = new Thread(this);
//		thread.start();
		
		// create provider manager
		Log.d(LOG_TAG, "Create ProviderManager");
		ProviderManager providerManager = ProviderManager.getInstance(this.getApplicationContext());
		
		Intent intent = getIntent();
		if (intent.getExtras() != null)
		{
			appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		
		Log.d(LOG_TAG, "Choose content for a widget with Id : " + appWidgetId);

		// create list of categories and adapter for it
		ListView categoryList = new ListView(this);
		categoryListAdapter = new CategoryListAdapter(
				providerManager.getCategories(), this, appWidgetId);
		categoryList.setAdapter(categoryListAdapter);
		categoryList.setOnItemClickListener(new OnCategoryClickListener(this,
				(CategoryListAdapter) categoryList.getAdapter()));
		setContentView(categoryList);
		
//		loadingDialog.dismiss();
	}

	@Override
	protected void onDestroy() {
		RateType rateType = categoryListAdapter.getSelectedRateType();
		if (rateType != null) {
			String rateTypeId = ProviderManager.getInstance(this.getApplicationContext()).getExternalIdByRateType(rateType);
			Log.d(LOG_TAG, "RateType: " + rateTypeId);
			
			WidgetProvider.registerWidget(this.getApplicationContext(), appWidgetId, rateTypeId);
			AppWidgetConfigure.saveTitlePref(this.getApplicationContext(), appWidgetId, rateTypeId);
			Log.d(LOG_TAG, "Save settings");
		}
		super.onDestroy();
	}

	@Override
	public void run() {
		// create provider manager
		Log.d(LOG_TAG, "Create ProviderManager");
		ProviderManager providerManager = ProviderManager.getInstance(this.getApplicationContext());
		
		Intent intent = getIntent();
		if (intent.getExtras() != null)
		{
			appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		
		Log.d(LOG_TAG, "Choose content for a widget with Id : " + appWidgetId);

		// create list of categories and adapter for it
		ListView categoryList = new ListView(this);
		categoryListAdapter = new CategoryListAdapter(
				providerManager.getCategories(), this, appWidgetId);
		categoryList.setAdapter(categoryListAdapter);
		categoryList.setOnItemClickListener(new OnCategoryClickListener(this,
				(CategoryListAdapter) categoryList.getAdapter()));
		setContentView(categoryList);
	}
}
